<?php
namespace app\common\library\helper;

use app\common\exception\NException;
use app\common\model\Sms;
use think\Request;

/**
 * Class SmsHelper
 * @package app\common\library\helper
 * @desc 短信
 */

class SmsHelper{

    /**
     * @param $event //事件类型（register, forget_password ....）
     * @param $mobile //手机号
     * @param string $code //验证码
     * @throws NException
     * @throws \Exception
     * @desc 发送验证码
     */
    public static function sendCode( $event, $mobile, $code = '' ){

        try{

            if( ! $code ){
                $code = mt_rand( 1000, 9999 );
            }

            //短信配置
            $config_sms = ConfigHelper::getConfig( 'sms' );

            $model_sms = new Sms();

            //用户规定时间内，该事件，发送的短信数量
            $count = $model_sms->getEventMobileCount( $mobile, $event );

            //如果超出规定次数
            if( $count >= $config_sms[ 'code_num_limit' ] ){
                throw new NException( '短信发送过于频繁' );
            }

            //发送短信
            self::_sendCaptcha( $mobile, $code );

            $data_sms = [
                'event'  => $event,
                'mobile' => $mobile,
                'code'   => $code,
                'ip'     => Request::instance()->ip(),
            ];

            Sms::create( $data_sms );

            return $data_sms;

        }catch( NException $e ){
            throw new NException( $e->getMessage() );
        }catch( \Exception $e ){
            Helper::logError( $e );
            throw new \Exception();
        }

    }

    /**
     * @param $event
     * @param $mobile
     * @param $code
     * @return Sms|array|false|\PDOStatement|string|\think\Model
     * @throws NException
     * @throws \Exception
     * @desc 检测手机验证码
     */
    public static function checkCode( $event, $mobile, $code ){

        try{

            $sms_config = ConfigHelper::getConfig( 'sms' );

            //验证码过期时间
            $code_expire_time = time() - $sms_config[ 'code_expire' ];

            //根据条件找到这条验证码
            $model_sms = new Sms();
            $model_sms = $model_sms
                ->where( [
                    'mobile'     => $mobile,
                    'event'      => $event,
                    'createtime' => [ 'gt', $code_expire_time ]
                ] )
                ->order( 'id DESC' )
                ->find();

            //如果数据不存在
            if( ! $model_sms ){

                throw new NException( '验证码错误' );

            }

            //如果验证码已销毁
            if( $model_sms[ 'is_destroy' ] == $sms_config[ 'code_is_destroy_yes' ] ){

                throw new NException( '验证码错误' );

            }

            //如果验证码正确
            if( $model_sms[ 'code' ] != $code ){

                //累加该手机号，该事件的验证次数
                $model_sms[ 'times' ]++;
                $model_sms->isUpdate( true )->force()->save();

                throw new NException( '验证码错误' );

            }

            //如果该手机号，该事件下的验证次数超出
            if( $model_sms[ 'times' ] > $sms_config[ 'code_check_times' ] ){

                throw new NException( '验证码错误' );

            }

            return $model_sms;

        }catch( NException $e ){
            throw new NException( $e->getMessage() );
        }catch( \Exception $e ){
            Helper::logError( $e );
            throw new \Exception();
        }

    }

    /**
     * @param $model_sms \app\common\model\Sms
     * @desc 销毁验证码
     */
    public static function destroyCode( $model_sms ){

        $sms_config = ConfigHelper::getConfig( 'sms' );

        $model_sms[ 'is_destroy' ] = $sms_config[ 'code_is_destroy_yes' ];

        $model_sms->isUpdate( true )->force()->save();

    }

    //调用第三方短信平台
    private static function _sendCaptcha( $mobile, $code ){

    }

}